package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.RolePermissionDTO;
import com.mrdeer.common.entity.sys.RolePermission;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.RolePermissionMapper;
import com.mrdeer.web.service.sys.SysRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysRolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements SysRolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<RolePermission> findByPermissionId(String permissionId) {
        RolePermissionDTO dto = new RolePermissionDTO();
        dto.setPermissionId(permissionId);
        return rolePermissionMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<RolePermission> findByRoleId(String roleId) {
        RolePermissionDTO dto = new RolePermissionDTO();
        dto.setRoleId(roleId);
        return rolePermissionMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public void deleteByRoleId(String roleId) {
        RolePermissionDTO dto = new RolePermissionDTO();
        dto.setRoleId(roleId);
        rolePermissionMapper.delete(fuzzyQuery(dto));
    }

    @Override
    public RolePermission add(RolePermission rolePermission) {
        if (rolePermissionMapper.insert(rolePermission) > 0) {
            return rolePermissionMapper.selectById(rolePermission.getId());
        }
        throw new BusinessException("add failed");
    }

    @Override
    public boolean delete(String id) {
        return rolePermissionMapper.deleteById(id) > 0;
    }

    @Override
    public RolePermission update(RolePermission rolePermission) {
        if (rolePermissionMapper.updateById(rolePermission) > 0) {
            RolePermission select = rolePermissionMapper.selectById(rolePermission.getId());
            return select;
        }
        throw new BusinessException("update failed!");
    }

    @Override
    public RolePermission getById(String id) {
        return rolePermissionMapper.selectById(id);
    }

    @Override
    public Page<RolePermission> list(RolePermissionDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return rolePermissionMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<RolePermission> fuzzyQuery(RolePermissionDTO dto) {
        LambdaQueryWrapper<RolePermission> queryWrapper = Wrappers.<RolePermission>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getRoleId())) {
                queryWrapper.eq(RolePermission::getRoleId, dto.getRoleId());
            }
            if (StrUtil.isNotBlank(dto.getPermissionId())) {
                queryWrapper.eq(RolePermission::getPermissionId, dto.getPermissionId());
            }
        }
        return queryWrapper;
    }
}
